package com.example.prom.aspect;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.Map;

@Slf4j
@Aspect
@Component
public class ThirdApiLogAspect {
    private static final String REGEX = "#.+?#";
    private final String REQUEST_MESSAGE_BODY = "请求入参:#方法名#:#请求体#";
    private final String RESPONSE_MESSAGE_BODY = "请求响应:#方法名#:#响应体#";
//    private final String REQUEST_MESSAGE_BODY = "请求入参:#方法名#:#请求路径#:请求入参:#请求体#";
//    private final String RESPONSE_MESSAGE_BODY = "请求响应:#方法名#:#请求路径#:响应出参:#响应体#";
    ServletInputStream inputStream = null;
//    com.example.prom.dangwen.y2022.service
//    @Pointcut("execution(* com.example.prom.ok.y2022.service.taxbit.*.*(..))")
    @Pointcut("execution(* com.example.prom.dangwen.y2022.service.*.*(..))")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object logPointAround(ProceedingJoinPoint pjp) throws Throwable {
        Signature signature = pjp.getSignature();
        String clzName = signature.getDeclaringTypeName().substring(signature.getDeclaringTypeName().lastIndexOf(".")+1,signature.getDeclaringTypeName().length());
        String methodDesc = clzName + "." + signature.getName();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return printLog(methodDesc, request, pjp);
    }

    private Object printLog(String methodDesc, HttpServletRequest request, ProceedingJoinPoint pjp) throws Throwable {
//        String method = request.getMethod();
//        if (method.equals("GET")|| method.equals("DELETE")) {
//            Map<String, String[]> parameterMap = request.getParameterMap();
//            Object attribute = request.getAttribute("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables");
//            System.out.println(JSON.toJSONString(attribute));
//            String content1 = replaceString(REQUEST_MESSAGE_BODY,methodDesc, request.getRequestURI(), JSON.toJSONString(parameterMap));
//            log.info(content1);
//        } else if (method.equals("POST") || method.equals("PUT")) {
//            //获取请求body
//            inputStream = request.getInputStream();
//            String body =  StreamUtils.copyToString(request.getInputStream(), Charset.forName(request.getCharacterEncoding()));
//            String content2 = replaceString(REQUEST_MESSAGE_BODY,methodDesc, request.getRequestURI(), body);
////            String content2 = replaceString(REQUEST_MESSAGE_BODY,methodDesc, request.getRequestURI(), JSON.toJSONString(pjp.getArgs()));
//            log.info(content2);
//        }
            String reqLogIngo = replaceString(REQUEST_MESSAGE_BODY,methodDesc, JSON.toJSONString(pjp.getArgs()));
            log.info(reqLogIngo);
        Object obj = pjp.proceed();

        String respLogInfo = replaceString(RESPONSE_MESSAGE_BODY,methodDesc, JSON.toJSONString(obj));
//        String content3 = replaceString(RESPONSE_MESSAGE_BODY,methodDesc, request.getRequestURI(), JSON.toJSONString(obj));
        log.info(respLogInfo);
        return obj;
    }
    public static String replaceString(String text, String ...values) {
        for (String value : values) {
            if(StringUtils.isBlank(value)) {
                value = "";
            }
            text = text.replaceFirst(REGEX,value);
        }
        return text;
    }
}
